package messages;

import java.awt.EventQueue;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import GUI.ReadComplaints;
import controllers.CustomerController;
import controllers.DBConnection;
import controllers.WorkerController;
import entity.UserLoginInfo;
import ocsf.server.ConnectionToClient;
import entity.User;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JOptionPane;

import org.apache.james.mime4j.field.datetime.DateTime;

import com.ibm.icu.text.DateFormat;

/**
 * this class should handle with all the messages that belong to the complaint from the DB
 */

public class ComplaintMessage {
    int CompId[];
	User user;
	ConnectionToClient client;
	String[] userArr;
	CustomerController customerC;
	SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	java.sql.Timestamp date;	
	int numOfComplaints;
	int complaintID;
	Date returndate,today1,date1;
	
	
	public ComplaintMessage(String[] strMsg, ConnectionToClient client){
		this.userArr=strMsg;
		this.client=client;
		//System.out.println("in Complaint message  "+ userArr[0]+"  "+userArr[1]+" "+userArr[1] );
	//	System.out.println("client info:  " +client.getId()+" " +client.getName()+" " +client.getInfo(null));
	}
	
	/**
	 * making connection with DB
	 * SELECT/UPDATE/INSERT from and to DB 
	 */
	
	public void activationComplaint(){
	
		//SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.x");
		//java.util.Date date = new java.util.Date();	
		//String s=dateTime.format(date);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		System.out.println(dateFormat.format(cal.getTime()));
		String s = null;
		if (cal != null) {
		s = dateFormat.format(cal.getTime());
		}
		
		//System.out.println("Date2..."+ s );
		System.out.println("test->" +s);
		//java.sql.Timestamp sqlDate7 = java.sql.Timestamp.valueOf(s);
		//System.out.println("sqlDate111:.. " + sqlDate7);
		
		try{
			
			String id=this.userArr[1];
			String complaintTxt=this.userArr[2];
			System.out.println(id);
			
			// get number of complaints of this customer = id of complaint 
			Statement stmt;
			DBConnection conn=new DBConnection();
			java.sql.PreparedStatement pr= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.users " + "WHERE ID ='" + userArr[1]+"';");
			System.out.println("numOfComplaints  ");
			ResultSet rs=pr.executeQuery();	
			if(!rs.next()){
				pr.close();
				rs.close();
				JOptionPane.showMessageDialog(null, "No match between id and password");
			
			}else
				
			this.setNumOfComplaints(rs.getInt("numOfComplaints"));
			System.out.println(numOfComplaints);
			this.complaintID=numOfComplaints+1;
			if (client != null) {
				client.sendToClient("getNumOfComplaints");
				} else{
				       System.out.println("client is null, do not attempt to initialize getNumOfComplaints");
				}
			
			//client.sendToClient("check_check");
			rs.close();
			pr.close();
			
			//update user complaints number			
			java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.users SET numOfComplaints=? WHERE ID=? ");
            pr1.setInt(1,complaintID);
            pr1.setString(2, id);
			pr1.executeUpdate();	
            System.out.println("UpdateNumOfComplaints");
            if (client != null) {
            	client.sendToClient("UpdateNumOfComplaints");
				} else{
				       System.out.println("client is null, do not attempt to initialize UpdateNumOfComplaints");
				}
			
			pr1.close();
					
			// update new complaint	in DB
			java.sql.PreparedStatement pr2 = conn.getDBConnection().prepareStatement("INSERT INTO sgi_system.complaints (customerID,complaintID,text,time,status) VALUES(?,?,?,?,?);");
			pr2.setString(1,id );
			pr2.setInt(2, complaintID);
			pr2.setString(3,complaintTxt );
			pr2.setString(4, s);
			pr2.setInt(5, 1);			
			pr2.executeUpdate();
			System.out.println("updateNewComplaint");
			System.out.println(client);
			  if (client != null) {
				  client.sendToClient("updateNewComplaint");
					} else{
					       System.out.println("client is null, do not attempt to initialize updateNewComplaint");
					}
			
			pr2.close();
			
		
			java.sql.PreparedStatement pr3= conn.getDBConnection().prepareStatement("SELECT text " + "FROM sgi_system.complaints ");
			ResultSet rs3=pr3.executeQuery();
			int i=0;
			//String tt;
	//		 tt = rs3.getString("text");
		//		System.out.println(" "+tt);
			/*while (rs3.next()){
			 tt[i] = rs3.getString("text");
			System.out.println(" "+tt[i]);
			i++;
			}*/
		}catch (Exception e)
	       {
	    	   e.printStackTrace();
	       
	       } 
	
		}
	
	
	
	
	
	public int getNumOfComplaints() 
	{
		return this.numOfComplaints;
	}


	public void setNumOfComplaints(int numOfComplaints) 
	{
		this.numOfComplaints = numOfComplaints;
	}
	
	
	
	
	
}
